#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a)
#define ff first
#define ss second 
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int inf = 1 << 28;
const ldb pi = fabsl(atan2l(0.0, -1.0));
using namespace std;


int n;
int fst[200010], nxt[300010], end[300010], hap[300010];
int edges;

void addEdge(int i, int j, int t)
{
	nxt[edges] = fst[i];
	fst[i] = edges;
	end[edges] = j;
	hap[edges++] = t;
	nxt[edges] = fst[j];
	fst[j] = edges;
	end[edges] = i;
	hap[edges++] = t;
}

bool happy(int a)
{
	while (a)
	{
		if ((a % 10) != 7 && (a % 10) != 4) return false;
		a /= 10;
	}
	return true;
}


void Load()
{
	cin >> n;
	int a, b, c;
	memset(fst, -1, sizeof fst);
	for (int i = 1; i <= n - 1; i++)
	{
		scanf("%d%d%d", &a, &b, &c);
		addEdge(a, b, happy(c));
	}
}

int was[200010];
int dhap[200010], d[200010];

void dfs(int v)
{
	was[v] = 1;
	d[v] = 1;
	for (int u = fst[v]; u != -1; u = nxt[u])
	{
		if (was[end[u]])
			continue;
		dfs(end[u]);
		d[v] += d[end[u]];
		if (hap[u])
			dhap[v] += d[end[u]];
		else dhap[v] += dhap[end[u]];
	}
}

LL res = 0;
LL up_happy;

void dfs2(int v)
{
	was[v] = 1;
	res += (LL)dhap[v] * (LL)(dhap[v] - 1);
	res += (LL)up_happy * dhap[v] * 2;
	res += (LL)up_happy * (LL)(up_happy - 1);
	int _uh;
	for (int u = fst[v]; u != -1; u = nxt[u])
	{
		if (was[end[u]]) continue;
		_uh = up_happy;
		if (hap[u])
			up_happy = n - d[end[u]];
		else
			up_happy = up_happy + dhap[v] - dhap[end[u]];
		dfs2(end[u]);
		up_happy = _uh;
	}
}

void Solve()
{
	dfs(1);
	memset(was, 0, sizeof was);
	dfs2(1);
	cout << res;
}

#define file "c"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	Load();
	Solve();
	return 0;
}
